package View.apply;

import Service.OutApplyService;
import Service.ServiceImpl.OutApplyServiceImpl;
import Service.ServiceImpl.systemMsgServiceImpl;
import Service.systemMsgService;
import domain.OutApply;
import domain.User;
import domain.systemMsg;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.PathIterator;
import java.util.Date;

public class OutApplyApprove extends JFrame {
    public OutApplyApprove(OutApply app, User user){
        init();
        componentInit(app,user);
    }
    public void init(){
        this.setTitle("外出申请审批");
        this.setSize(482,438);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(OutApply app,User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("学号:");
        label1.setBounds(97,52,65,30);
        JLabel label2= new JLabel();
        label2.setText("姓名:");
        label2.setBounds(96,101,65,30);
        JLabel label3= new JLabel();
        label3.setText("外出原因:");
        label3.setBounds(94,159,65,30);
        JLabel label4= new JLabel();
        label4.setText("外出时间:");
        label4.setBounds(96,213,65,30);
        JLabel label5= new JLabel();
        label5.setText("审批人:");
        label5.setBounds(94,261,65,30);

        //------------------JTextField--------------------
        JTextField field1= new JTextField();
        field1.setText(app.getStunum());
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setEnabled(false);
        field1.setBounds(177,51,120,30);
        JTextField field2= new JTextField();
        field2.setText(app.getName());
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setEnabled(false);
        field2.setBounds(178,100,120,30);
        JTextField field3= new JTextField();
        field3.setText(app.getOutReason());
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setEnabled(false);
        field3.setBounds(175,157,120,30);
        JTextField field4= new JTextField();
        field4.setText(String.valueOf(app.getLeftTime()));
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setEnabled(false);
        field4.setBounds(174,212,145,30);
        JTextField field5= new JTextField();
        field5.setText("");
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(172,262,120,30);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("通过");
        button1.setBounds(70,331,95,27);
        JButton button3= new JButton();
        button3.setText("不通过");
        button3.setBounds(170,331,95,27);
        JButton button2= new JButton();
        button2.setText("退出");
        button2.setBounds(270,331,95,27);





        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(label5);
        this.add(field1);
        this.add(field2);
        this.add(field3);
        this.add(field4);
        this.add(field5);
        this.add(button1);
        this.add(button2);
        this.add(button3);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                OutApplyService service = new OutApplyServiceImpl();
                service.updateApplyStateById(app.getId(),"true",field5.getText());

                systemMsg msg = new systemMsg();
                msg.setUsername(field2.getText());
                msg.setTitle("外出审批");
                msg.setNeirong("学生:"+field2.getText()+" 学号:"+ field1.getText()+" 你的外出审批已通过。");
                msg.setSendTime(new Date());
                msg.setIsread("false");

                systemMsgService service1 = new systemMsgServiceImpl();
                service1.addMsg(msg);

                JOptionPane.showMessageDialog(new JFrame(),"提交成功!");
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new OutApplyPage(user).setVisible(true);
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                OutApplyService service = new OutApplyServiceImpl();
                service.updateApplyStateById(app.getId(),"false",field5.getText());

                systemMsg msg = new systemMsg();
                msg.setUsername(field2.getText());
                msg.setTitle("外出审批");
                msg.setNeirong("学生:"+field2.getText()+" 学号:"+field1.getText()+" 你的外出审批未通过请重新提交。");
                msg.setSendTime(new Date());
                msg.setIsread("false");

                systemMsgService service1 = new systemMsgServiceImpl();
                service1.addMsg(msg);

                JOptionPane.showMessageDialog(new JFrame(),"提交成功!");
            }
        });
    }
}
